import { useUserStore } from '@/stores/user'

/**
 * 权限控制指令
 * 使用方式：v-permission="'admin'" 或 v-permission="['admin', 'editor']"
 */
export default {
  mounted(el, binding) {
    const userStore = useUserStore()
    const { value } = binding
    
    // 获取用户权限
    const userPermissions = userStore.userPermissions || []
    const userRoles = userStore.userRoles || []
    
    // 判断是否有权限
    const hasPermission = () => {
      if (value === undefined || value === null || value === '') {
        return true
      }
      
      if (typeof value === 'string') {
        return userPermissions.includes(value) || userRoles.includes(value)
      }
      
      if (Array.isArray(value)) {
        return value.some(permission => 
          userPermissions.includes(permission) || userRoles.includes(permission)
        )
      }
      
      return false
    }
    
    // 如果没有权限，则移除元素
    if (!hasPermission()) {
      el.parentNode && el.parentNode.removeChild(el)
    }
  }
}